package m202405.d14;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/14 18:34
 */
public class Q347 {
    public int[] topKFrequent(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            map.put(num, map.getOrDefault(num, 0) + 1);
        }
        PriorityQueue<Map.Entry<Integer, Integer>> queue = new PriorityQueue<>(k, (v1, v2) -> v2.getValue() - v1.getValue());
        queue.addAll(map.entrySet());
        int[] res = new int[k];
        for (int i = 0; i < res.length && !queue.isEmpty(); i++) {
            res[i] = queue.poll().getKey();
        }
        return res;
    }
}
